﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ControlModule.Daos;
using ControlModule.Models;

namespace ControlModule.QuizModule
{
    public class QuizManager
    {
        private static QuizManager _instance;
        private QuizManager() { }
        public static QuizManager Get()
        {
            return _instance ?? (_instance = new QuizManager());
        }

        private List<QuizTestItem> _quizTestItems = null;
        private List<QuizTestItem>.Enumerator _enumerator;

        public bool IsQuestionLoaded()
        {
            return _quizTestItems != null;
        }

        public void ClearLoadedQuestion()
        {
            _quizTestItems = null;
        }

        public void LoadQuestionForLabwork(int labworkId)
        {
            _quizTestItems =
                QuestionDao.LoadQuestionByLabworkId(labworkId).Select(
                question => new QuizTestItem
                {
                    Id = question.Id,
                    Data = question.Data,
                    Answers = AnswerDao.LoadAnswersByQuestionId(question.Id)
                }).ToList();
            _enumerator = _quizTestItems.GetEnumerator();
        }

        public QuizTestItem ReadNext()
        {
            if (_enumerator.MoveNext())
                return _enumerator.Current;
            return null;
        }
    }
}